package com.wxy.controller;

import com.wxy.domain.R;
import com.wxy.service.UpdateUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * @author 47868
 */
@RestController
@RequestMapping("updateUser")
public class UpdateUserController {
    private final UpdateUserService updateUserService;

    @Autowired
    public UpdateUserController(UpdateUserService updateUserService) {
        this.updateUserService = updateUserService;
    }

    @Operation(summary = "用户更新用户名",description = "用户更新用户名")
    @ApiResponse(responseCode = "200",description = "成功",content = @Content)
    @PostMapping("update-username")
    public R updateUsername(@RequestParam("username") String newUsername,
                            HttpServletRequest request) {
        Map<String,Object> usermap=(Map<String, Object>) request.getAttribute("body");
        int id=(int)usermap.get("userId");
        return updateUserService.updateUsername(newUsername,id);
    }

    @Operation(summary = "更新邮箱",description = "用户更新邮箱")
    @ApiResponse(responseCode = "200",description = "更新邮箱成功",content = @Content)
    @PostMapping("update-email")
    @ResponseBody
    public R updateEmail(@RequestParam("email") String newEmail,
                         HttpServletRequest request){
        Map<String,Object> usermap=(Map<String, Object>) request.getAttribute("body");
        int id=(int)usermap.get("userId");
        return updateUserService.updateEmail(newEmail,id);
    }

    @Operation(summary = "验证密码",description = "用户修改密码之前验证密码")
    @ApiResponse(responseCode = "200",description = "成功",content = @Content)
    @PostMapping("verify-password")
    public R verifyPassword(@RequestParam("oldPassword") String oldPassword,HttpServletRequest request) {
        Map<String,Object> usermap=(Map<String, Object>) request.getAttribute("body");
        int id=(int)usermap.get("userId");
        return updateUserService.verifyPassword(oldPassword,id);
    }

    @Operation(summary = "修改密码",description = "用户修改密码")
    @ApiResponse(responseCode = "200",description = "成功",content = @Content)
    @PostMapping("change-password")
    public R changePassword(@RequestParam("newPassword") String newPassword,HttpServletRequest request) {
        Map<String,Object> usermap=(Map<String, Object>) request.getAttribute("body");
        int id=(int)usermap.get("userId");
        return updateUserService.changePassword(newPassword,id);
    }

    @Operation(summary = "更新头像",description = "用户更新头像信息")
    @ApiResponse(responseCode = "200" ,description = "更新成功",content = @Content)
    @PostMapping("update-avatar")
    @ResponseBody
    public R updateAvatar(@RequestParam("avatar") MultipartFile avatar,HttpServletRequest request) {
        Map<String,Object> usermap=(Map<String, Object>) request.getAttribute("body");
        int id=(int)usermap.get("userId");
        return updateUserService.updateAvatar(avatar,id);
    }
}
